package com.atguigu.gulimall.product.service.impl;

import com.atguigu.gulimall.product.vo.BrandVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.product.dao.CategoryBrandRelationDao;
import com.atguigu.gulimall.product.entity.CategoryBrandRelationEntity;
import com.atguigu.gulimall.product.service.CategoryBrandRelationService;
import org.springframework.transaction.annotation.Transactional;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryBrandRelationEntity> getCategoryBrandRelationList(Map<String, Object> params) {
        //首先获取查询的品牌id
        String brandId = params.get("brandId").toString();
        LambdaQueryWrapper<CategoryBrandRelationEntity> lambdaQueryWrapper = new LambdaQueryWrapper<CategoryBrandRelationEntity>();
        lambdaQueryWrapper.eq(CategoryBrandRelationEntity::getBrandId, brandId);
        List<CategoryBrandRelationEntity> list = this.list(lambdaQueryWrapper);
        return list;
    }

    /**
     * 当品牌名称进行修改的时候将关系表中保存的品牌名称也进行修改
     * @param brandId
     * @param name
     */
    @Override
    public void updateBrand(Long brandId, String name) {
        //创建一个实体类进行数据修改
        CategoryBrandRelationEntity entity = new CategoryBrandRelationEntity();
        //进行赋值
        entity.setBrandName(name);
        entity.setBrandId(brandId);
        //设置更新条件
        LambdaQueryWrapper<CategoryBrandRelationEntity> lambdaQuery = new LambdaQueryWrapper<CategoryBrandRelationEntity>();
        lambdaQuery.eq(CategoryBrandRelationEntity::getBrandId,brandId);
        //执行修改
        this.update(entity,lambdaQuery);

    }

    /**
     * 当分类名称进行修改的时候将关系表中保存的分类名称也进行修改
     * @param catId
     * @param name
     */
    @Override
    public void updateCategory(Long catId, String name) {
        //创建一个实体类进行数据修改
        CategoryBrandRelationEntity entity = new CategoryBrandRelationEntity();
        //进行赋值
        entity.setCatelogName(name);
        entity.setCatelogId(catId);
        //设置更新条件
        LambdaQueryWrapper<CategoryBrandRelationEntity> lambdaQuery = new LambdaQueryWrapper<CategoryBrandRelationEntity>();
        lambdaQuery.eq(CategoryBrandRelationEntity::getCatelogId,catId);
        //执行修改
        this.update(entity,lambdaQuery);
    }

    @Override
    public List<CategoryBrandRelationEntity> getBrandsLists(Long catId) {
        //创建查询条件
        LambdaQueryWrapper<CategoryBrandRelationEntity> lambdaQueryWrapper = new LambdaQueryWrapper<CategoryBrandRelationEntity>();
        //赋值查询条件
        lambdaQueryWrapper.eq(CategoryBrandRelationEntity::getCatelogId, catId);
        //进行查询
        List<CategoryBrandRelationEntity> list = this.list(lambdaQueryWrapper);
        return list;
    }

}